package com.wangdao.wechatmall.controller.wx;

import com.wangdao.wechatmall.bean.vo.admin.BaseRespVo;
import com.wangdao.wechatmall.bean.vo.wx.SearchIndexVO;
import com.wangdao.wechatmall.mapper.UserMapper;
import com.wangdao.wechatmall.service.admin.SearchService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ShuiYi
 * @time 2021-07-12 11:20
 */
@RestController
@RequestMapping("wx/search")
public class WXSearchController {
    @Autowired
    SearchService searchService;
    @Autowired
    UserMapper userMapper;
    @RequestMapping("index")
    public BaseRespVo index() {
        String username = (String) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        int id = userMapper.queryId(username);
        SearchIndexVO searchIndexVO = searchService.index(id);
        return BaseRespVo.ok(searchIndexVO);
    }

    @RequestMapping("helper")
    public BaseRespVo helper(String keyword) {
        List<String> rets = searchService.searchHelper(keyword);

        return BaseRespVo.ok(rets);
    }

    @RequestMapping("clearhistory")
    public BaseRespVo clearHistory() {
        String username = (String) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        int id = userMapper.queryId(username);
        searchService.clearHistory(id);
        return BaseRespVo.ok();
    }
}
